package com.course.action;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import com.course.bean.FeedbackTo;
import com.course.delegate.FeedbackMgrDelegate;
import com.course.exception.ConnectionException;
import com.course.exception.LoginException;
import com.course.formbean.FeedBackFormBean;
import com.course.util.UtilConstants;




public class PostFeedBackAction extends HttpServlet {

	
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
             doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FeedBackFormBean fb=new FeedBackFormBean();
		
		HttpSession hs=request.getSession();
		hs.setAttribute("feedbackbean", fb);
		Map map=request.getParameterMap();
	try {
		BeanUtils.populate(fb, map);
	} catch (IllegalAccessException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		
		
	}
        String path="";
        boolean flag=false;

	  try{
		  
		  FeedbackTo fto=new FeedbackTo(fb);
		  try{
		  
		   flag= new FeedbackMgrDelegate().insertFeedBack(fto);
		  }catch (LoginException e) {
			e.printStackTrace();
		}catch (ConnectionException e) {
			e.printStackTrace();
		}
		   if(flag==true){
			  request.setAttribute("status", UtilConstants._INSERT_FEEDBACK);
			  path=UtilConstants._FEEDBACK_HOME;
			 }
		  else {
			  request.setAttribute("status", UtilConstants._FEEDBACK_FAILED);
			  path=UtilConstants._FEEDBACK_HOME;
			  }
		  }
	  catch (Exception e) {
		  request.setAttribute("status", UtilConstants._INVALIED_DATA);
		  path=UtilConstants._FEEDBACK_HOME;
		e.printStackTrace();
		}
     RequestDispatcher rd=request.getRequestDispatcher(path);
		rd.forward(request,response);
	
	
	
	}
}
